package com.apanal.qlife.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.UserService;

/**
 * 用户控制类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-7-21下午5:20:44
 */
@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService service;

	/**
	 * 分页查询后台用户
	 * 
	 * @param user
	 * @param page
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:user:view")
	@SystemLog(module = "后台用户管理", operation = "分页查询用户", type = LogType.SELECT)
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(User user, Page page, Model model) {
		PageReturn<User> result = service.selectRelationForPage(user, page);
		model.addAttribute(Constants.DEFAULT_RESULT_NAME, result);
		return "sys/userList";
	}

	/**
	 * 转向搜索弹出框页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toListOpen", method = RequestMethod.GET)
	public String toListOpen() {
		return "sys/userListOpen";
	}

	/**
	 * 分页查询后台用户,json返回
	 * 
	 * @param user
	 * @param page
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "listJson", method = RequestMethod.GET)
	public PageReturn<User> listJson(User user, Page page, Model model) {
		return service.selectRelationForPage(user, page);
	}

	/**
	 * 转向新增页面
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:user:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toCreate(Model model) {
		model.addAttribute("user", new User());
		model.addAttribute("op", "新增用户");
		return "sys/userEdit";
	}

	/**
	 * 新增
	 * 
	 * @param user
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "后台用户管理", operation = "新增用户", type = LogType.CREATE)
	@RequiresPermissions("users:user:create")
	@ResponseBody
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@CurrentUser User currUser, User user) {
		user.setInsertBy(currUser.getUseId());
		return service.insert(user);
	}

	/**
	 * 转向更新页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:user:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toUpdate(@PathVariable("id") Long id, Model model) {
		model.addAttribute("user", service.selectRelationById(id));
		model.addAttribute("op", "修改用户");
		return "sys/userEdit";
	}

	/**
	 * 更新
	 * 
	 * @param user
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "后台用户管理", operation = "修改用户", type = LogType.UPDATE)
	@RequiresPermissions("users:user:update")
	@ResponseBody
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(@CurrentUser User currUser, User user) {
		user.setLastUpdateBy(currUser.getUseId());
		return service.updateByIdSelective(user);
	}

	/**
	 * 转向详情页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:user:view")
	@RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
	public String toDetail(@PathVariable("id") Long id, Model model) {
		model.addAttribute("user", service.selectRelationById(id));
		model.addAttribute("op", "查看用户");
		return "sys/userDetail";
	}

	/**
	 * 硬删除
	 * 
	 * @param user
	 * @param id
	 * @return
	 */
	@SystemLog(module = "后台用户管理", operation = "删除用户", type = LogType.DELETE)
	@RequiresPermissions("users:user:delete")
	@ResponseBody
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable("id") Long id) {
		return service.deleteById(id);
	}

	/**
	 * 解锁/锁定
	 * 
	 * @param user
	 * @param user
	 * @return
	 */
	@SystemLog(module = "后台用户管理", operation = "锁定/解锁用户", type = LogType.UPDATE)
	@RequiresPermissions("users:user:start/stop")
	@ResponseBody
	@RequestMapping(value = "/{useId}/{useLocked}/updateToLockOrUnLock", method = RequestMethod.GET)
	public String updateToLockOrUnLock(@CurrentUser User currUser,
			@PathVariable("useId") Long useId,
			@PathVariable("useLocked") Integer useLocked) {
		return service.updateToLockOrUnLock(useId, useLocked,
				currUser.getUseId());
	}

	/**
	 * 检查手机号码是否存在,修改时排除当前手机号码
	 * 
	 * @param useId
	 *            用户id,新增时传0
	 * @param mobile
	 *            手机号吗
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{useId}/{mobile}/checkMobileExist", method = RequestMethod.GET)
	public Boolean checkMobileExist(@PathVariable("useId") Long useId,
			@PathVariable("mobile") String mobile) {
		return service.checkMobileExist(useId, mobile);
	}

	/**
	 * 检查旧密码是否相同
	 * 
	 * @param password
	 *            密码
	 * @return 0:密码不一致 1:密码一致
	 */
	@ResponseBody
	@RequestMapping(value = "/{password}/checkPwdEqual", method = RequestMethod.GET)
	public int checkPwdEqual(@CurrentUser User currUser,
			@PathVariable("password") String password) {
		return service.checkPwdEqual(currUser.getUseId(), password);
	}

	/**
	 * 根据id字符串查询
	 * 
	 * @param ids
	 *            逗号分隔
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{ids}/selectByIds", method = RequestMethod.GET)
	public List<User> selectByIds(@PathVariable("ids") String ids) {
		return service.selectByIds(ids);
	}

	/**
	 * 重置密码
	 * 
	 * @param currUser
	 * @param user
	 * @return
	 */
	@SystemLog(module = "后台用户管理", operation = "重置密码", type = LogType.UPDATE)
	@RequiresPermissions("users:user:reset")
	@ResponseBody
	@RequestMapping(value = "/{id}/resetPwd", method = RequestMethod.GET)
	public String resetPwd(@CurrentUser User currUser,
			@PathVariable("id") Long id) {
		return service.resetPwd(id, currUser.getUseId());
	}

	/**
	 * 转向修改密码页面
	 * 
	 * @param currUser
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/updatePwd", method = RequestMethod.GET)
	public String toUpdatePwd() {
		return "sys/updatePwd";
	}

	/**
	 * 修改密码
	 * 
	 * @param currUser
	 * @param user
	 * @return
	 */
	@SystemLog(module = "后台用户管理", operation = "修改密码", type = LogType.UPDATE)
	@ResponseBody
	@RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
	public String updatePwd(@CurrentUser User currUser, String newPassword) {
		return service.changePassword(currUser.getUseId(), newPassword);
	}

	/**
	 * 根据验证码修改后台用户密码
	 * 
	 * @param mobile
	 *            手机号码
	 * @param newPassword
	 *            密码
	 * @param code
	 *            验证码
	 * @return
	 */
	@SystemLog(module = "后台用户管理", operation = "根据验证码修改密码", type = LogType.UPDATE)
	@ResponseBody
	@RequestMapping(value = "/updatePwdByCode", method = RequestMethod.POST)
	public String updatePwdByCode(String mobile, String newPassword, String code) {
		return service.changePwdByCode(mobile, newPassword, code,
				Constants.USER_TYPE_ADMIN);
	}
}
